package com.ly.emp.excelListener;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.ly.common.core.utils.excel.ExcelListener;
import com.ly.common.core.utils.excel.ExcelResult;
import com.ly.common.core.utils.SpringUtils;
import com.ly.emp.bean.dto.HubTeamDTO;
import com.ly.emp.bean.dto.TeamResultOrderDTO;
import com.ly.emp.bean.dto.TeamResultOrderDetailDTO;
import com.ly.emp.entity.EpmBaseRel;
import com.ly.emp.service.IEmpCommonService;
import com.ly.emp.service.IHubTeamResultOrderService;
import com.ly.emp.util.TeamValidatorUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 工单-导入
 */
@Slf4j
public class HubTeamResultOrderListener extends AnalysisEventListener<HubTeamResultOrderExcelVO> implements ExcelListener<HubTeamResultOrderExcelVO> {

    private final IHubTeamResultOrderService service;
    private final IEmpCommonService teamCommonService;
    private final Boolean isUpdateSupport;
    private final HubTeamDTO dto;

    private final StringBuilder successMsg = new StringBuilder();
    private final StringBuilder failureMsg = new StringBuilder();
    private int successNum = 0;
    private int failureNum = 0;
    private int startNum = 1;

    public HubTeamResultOrderListener(Boolean isUpdateSupport, HubTeamDTO dto) {
        this.service = SpringUtils.getBean(IHubTeamResultOrderService.class);
        this.teamCommonService = SpringUtils.getBean(IEmpCommonService.class);
        this.isUpdateSupport = isUpdateSupport;
        this.dto = dto;
    }

    @Override
    public void invoke(HubTeamResultOrderExcelVO vo, AnalysisContext context) {
        try {
            // 清空数据
            // if(isUpdateSupport){
            //     service.remove(new LambdaQueryWrapper<>());
            // }
            TeamValidatorUtils.validate(vo);
            TeamResultOrderDTO entity = new TeamResultOrderDTO();
            List<TeamResultOrderDetailDTO> teamWorkDetails = new ArrayList<>();
            TeamResultOrderDetailDTO teamResultOrderDetailDTO = BeanUtil.toBean(vo, TeamResultOrderDetailDTO.class);
            teamWorkDetails.add(teamResultOrderDetailDTO);
            // 设置主对象
            entity.setAddMode("10");
            entity.setEndDate(new Date());
            entity.setDetails(teamWorkDetails);
            // 封装用户信息
            String psnName = vo.getPsnName();
            startNum++;
            if (StrUtil.isNotBlank(psnName)) {
                EpmBaseRel baseRel = new EpmBaseRel();
                baseRel.setPsnName(psnName);
                baseRel.setOrgCode(dto.getOrgCode());
                EpmBaseRel baseRelTemp = teamCommonService.getBaseRelByParam(baseRel);
                if (baseRelTemp != null) {
                    BeanUtil.copyProperties(baseRelTemp, entity, new String[]{"seqKey"});
                    entity.setPeriodCode(dto.getPeriodCode());
                    // entity.setDisplayOrder(successNum);
                    service.save(entity);
                    successNum++;
                }
            }
        } catch (Exception e) {
            failureNum++;
            String msg = "<br/>" + failureNum + "、数据导入失败：";
            failureMsg.append(msg).append(e.getMessage());
            log.error(msg, e);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
    }

    @Override
    public ExcelResult<HubTeamResultOrderExcelVO> getExcelResult() {
        return new ExcelResult<HubTeamResultOrderExcelVO>() {

            @Override
            public String getAnalysis() {
                if (failureNum > 0) {
                    failureMsg.insert(0, "导入失败！共 " + failureNum + " 条数据导入失败，请检查！");
                    return failureMsg.toString();
                } else {
                    successMsg.insert(0, "导入成功！共 " + successNum + " 条数据已导入！");
                    return successMsg.toString();
                }
            }

            @Override
            public List<HubTeamResultOrderExcelVO> getList() {
                return null;
            }

            @Override
            public List<String> getErrorList() {
                return null;
            }
        };
    }
}
